import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;


public class UnaryRuleTranslator 
{
	static CategoryAnalyst c = new CategoryAnalyst(); 
	static String fileName = "UnaryRules.data";
	Hashtable<String, UnaryRule> data;
	
	
	public UnaryRuleTranslator()
	{
		data = new Hashtable<String, UnaryRule>();
		try 
		{
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			while(in.ready())
			{
				
				String input = in.readLine();
				int at = input.indexOf("@");
				String oldCat = input.substring(0,at);
				String newCat = input.substring(at+1,input.length());
				UnaryRule u;
				if(data.containsKey(oldCat))
				{
					u = data.remove(oldCat);
					u.addNewCat(newCat);
					data.put(oldCat, u);
				}
				else
				{
					u = new UnaryRule(oldCat,newCat);
					data.put(oldCat,u);
				}
				
			}
		} 
		catch (FileNotFoundException e) 
		{
			System.err.println("UnaryRuleTranslator: Error: "+ fileName + " not found.");
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public ArrayList<Sign> translate(Sign input)
	{
		UnaryRule u = data.get(input.getSuperDuperTag().toStringForComp());
		
		if(u == null)
			return new ArrayList<Sign>();
		
		ArrayList<String> subResult = u.getNewCat();
		ArrayList<Sign> result = new ArrayList<Sign>();
		for(String newCategory : subResult)
		{
			String newCategoryWithSemantics = c.addSemanticInfo(newCategory);
			Sign spotlight = new Sign(input);
			spotlight.setSuperDuperTag(new SuperDuperTag(newCategoryWithSemantics));
			for(Sign h : input.getHeads())
			{
				spotlight.addHead(h);
			}
			spotlight.setCreator("Special Unary: " + input.getSuperDuperTag().toStringForComp() + " --> " + newCategoryWithSemantics);
			Dependency d = new Dependency(input,input,spotlight,0);
			spotlight.addToParglist(d);
			result.add(spotlight);
		}
		return result;
	}
	
	
}
